package com.mini.cn.goods.servlet;


import com.mini.cn.goods.io.netty.ws.WebSocketNettyServer;
import com.mini.cn.goods.servlet.ws.PushServerListener;
import com.mini.cn.goods.servlet.ws.PushWsHandle;
import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * User: fly
 * Date: 2020-02-06
 * Time: 10:54 下午
 */

/**
 * websocket 服务启动
 */
@Slf4j
public enum WebSocketService {

    INSTANCE;

    private WebSocketNettyServer webSocketNettyServer;
    private final PushWsHandle pushWsHandle;
    private final PushServerListener pushServerListener;

    WebSocketService() {
        pushWsHandle = new PushWsHandle();
        webSocketNettyServer = new WebSocketNettyServer(pushWsHandle, null);
        pushServerListener = new PushServerListener();
        webSocketNettyServer.addServiceListener(pushServerListener);
    }

    public void startWsService(String path, int port) {
        log.info("startWsService ...");
        webSocketNettyServer.setPort(port);
        webSocketNettyServer.addPaths(new String[]{path});
        webSocketNettyServer.init().start();
        log.info("ws service start success port:" + webSocketNettyServer.getPort());
    }

    public void stopWsService() {
        webSocketNettyServer.shutDown();
    }
}
